_package("alz.gesture");

_import("alz.gesture.Gesture");

/**
 * 刷划手势
 */
_class("Swipe", Gesture, function(){
	this.handles = ["swipe"/*, "swipeup", "swipedown"*/, "swipeleft", "swiperight"];
	this.listenForEnd = false;
	this.swipeThreshold = 35;
	this.swipeTime = 1000;
	this._init = function(config){
		_super._init.apply(this, arguments);
	};
	this.onTouchStart = function(ev, touch){
		this.startTime = ev.timeStamp;
		this.startX = touch.pageX;
		this.startY = touch.pageY;
		this.lock("scroll", "scrollstart", "scrollend");
	};
	this.onTouchMove = function(ev, touch){
		var deltaY = touch.pageY - this.startY,
			deltaX = touch.pageX - this.startX,
			absDeltaY = Math.abs(deltaY),
			absDeltaX = Math.abs(deltaX),
			deltaTime = ev.timeStamp - this.startTime;
		/*
		console.log("this.swipeThreshold=" + this.swipeThreshold
			+ ",deltaX=" + deltaX
			+ ",deltaY=" + deltaY
			+ ",absDeltaX=" + absDeltaX
			+ ",absDeltaY=" + absDeltaY);
		*/
		// If the swipeTime is over, we are not gonna check for it again
		if(absDeltaY - absDeltaX > 3 || deltaTime > this.swipeTime){
			this.unlock("scroll", "scrollstart", "scrollend");
			this.stop();
		}else if(absDeltaX > this.swipeThreshold && absDeltaX > absDeltaY){
			// If this is a swipe, a scroll is not possible anymore
			this.fire("swipe", ev, {
				"direction": (deltaX < 0) ? "left" : "right",
				"distance" : absDeltaX,
				"deltaTime": deltaTime,
				"deltaX"   : deltaX
			});
			this.stop();
		}/*else if(absDeltaY > this.swipeThreshold && absDeltaX < absDeltaY){
			// If this is a swipe, a scroll is not possible anymore
			this.fire("swipe", ev, {
				"direction": (deltaY < 0) ? "up" : "down",
				"distance" : absDeltaY,
				"deltaTime": deltaTime,
				"deltaX"   : deltaY
			});
			this.stop();
		}*/
	};
});